<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.js"></script>
    <script src="./city_code.js"></script>
    <style>
      .active {
        color: white;
        background-color: black;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <select v-model="province">
        <option v-for="provinceItem in cityCode">{{provinceItem.name}}</option>
      </select>
      <select v-model="city">
        <option
          v-for="cityItem in cityCode.find(item=>item.name==province).city"
        >
          {{cityItem.name}}
        </option>
      </select>
      <select v-model="area">
        <option
          v-for="areaItem in cityCode.find(item=>item.name==province).city.find(item=>item.name==city).area"
        >
          {{areaItem.name}}
        </option>
      </select>
    </div>
  </body>
  <script>
    console.log(data);
    let app = Vue.createApp({
      data() {
        return {
          cityCode: data,
          province: "河南省", //省份
          city: "", //市
          area: "", //区
        };
      },
      watch: {
        // 监听省份变化 给市设置初始值
        province: {
          immediate: true,
          handler(val) {
            // 更新市
            this.city = this.cityCode.find(
              (item) => item.name == val
            ).city[0].name;
            this.area = "市辖区";
          },
        },
        city: {
          immediate: true,
          handler() {
            this.area = "市辖区";
          },
        },
      },
    });

    // mounted  beforeUnmount
    app = app.mount("#app");
  </script>
</html>
